package com.liuyk.commonlib.utility;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * CommonUtils
 * <p>
 * Created by liuyakui on 2020/5/5.
 */
public final class CommonUtils {

    //获取系统剪贴板服务
    public static String getClipboard(Context context) {
        try {
            ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            if (null != clipboardManager) {
                if (!clipboardManager.hasPrimaryClip()) {
                    return "";
                }
                // 获取剪贴板的剪贴数据集
                ClipData clipData = clipboardManager.getPrimaryClip();
                if (null != clipData && clipData.getItemCount() > 0) {
                    // 从数据集中获取（粘贴）第一条文本数据
                    ClipData.Item item = clipData.getItemAt(0);
                    if (null != item) {
                        return item.getText().toString();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void copyText(Context context, String str) {
        final ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (manager != null) {
            manager.setPrimaryClip(ClipData.newPlainText(null, str));
        }
    }

    /**
     * 解析Color
     *
     * @param colorString  color 例如从后端配置的
     * @param defaultColor 如果color有误，默认color
     * @return int color
     */
    public static int parseColor(String colorString, String defaultColor) {
        int color;
        try {
            color = Color.parseColor(colorString);
        } catch (Exception e) {
            color = Color.parseColor(defaultColor);
        }
        return color;
    }

    public static float parseFloat(String s, float defaultValue) {
        float value;
        try {
            value = Float.parseFloat(s);
        } catch (Exception e) {
            value = defaultValue;
        }
        return value;
    }

    public static long parseLong(String s, long defaultValue) {
        long value;
        try {
            value = Long.parseLong(s);
        } catch (Exception e) {
            value = defaultValue;
        }
        return value;
    }

    public static int parseInt(String s, int defaultValue) {
        int value;
        try {
            value = Integer.parseInt(s);
        } catch (Exception e) {
            value = defaultValue;
        }
        return value;
    }

    /**
     * 收起键盘
     */
    public static void hideSoftKeyboard(Activity activity) {
        View view = activity.getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 显示键盘
     */
    public static void showSoftKeyboard(Activity activity, EditText et) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
        imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
    }
}
